import { messages } from "share/common";
import React, { Component } from "react";

import { Checkbox, Button, Spin, message } from "antd";
import HotelServer from "../hotel-server";

class useMode extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isUse: false, // 启用
      loading: false,
      hotelData: {}, // 返回的酒店设置数据
    };
  }

  componentWillMount() {
    this.initData();
  }

  initData = () => {
    HotelServer(this)
      .getPlatformModelData({ companyOid: this.props.companyOID })
      .then((res) => {
        let isUse = res.data.body.useMode === "pop";
        this.setState({ isUse: isUse, hotelData: res.data.body });
      });
  };

  isOpen = (value) => {
    console.log("is use " + value);
    this.setState({ isUse: value.target.checked });
  };

  saveSetting = () => {
    const { hotelData, isUse } = this.state;
    const paramsData = Object.assign({}, hotelData);
    paramsData.useMode = isUse ? "pop" : hotelData.useMode;
    paramsData.companyOid = this.props.companyOID;
    HotelServer(this)
      .savePlatformModelData(paramsData)
      .then((res) => {
        if (res.data.success) {
          this.initData();
          message.success(messages("supplier-1.key355") /*保存成功*/);
        } else {
          message.error(res.data.message);
        }
      });
  };

  render() {
    const { isUse, loading } = this.state;
    return (
      <div className="airline-config-container">
        <Spin spinning={loading}>
          <h2>{messages("supplier-1.key483") /*平台模式*/}</h2>
          <a className="a-line-bottom">{messages("supplier-1.key510") /*服务由启用的TMC提供*/}</a>
          <Checkbox checked={isUse} onChange={this.isOpen}>
            {messages("supplier-1.key35") /*启用temp*/}
          </Checkbox>
          <br />
          <br />
          <h2>{messages("supplier-1.key511") /*汇联易自营模式*/}</h2>
          <a className="a-line-bottom">{messages("supplier-1.key512") /*由汇联易直接提供服务，开票等企业服务*/}</a>
          <p>{messages("supplier-1.key513") /*若需启动自营模式，请联系汇联易客户经理*/}</p>
          <br />
          <Button type="primary" onClick={this.saveSetting}>
            {messages("supplier-1.key356") /*保存配置*/}
          </Button>
        </Spin>
      </div>
    );
  }
}

export default useMode;
